package com.ruoyi.admin.service.impl;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


import com.ruoyi.admin.domain.Experimentrequest;
import com.ruoyi.admin.domain.Semester;
import com.ruoyi.admin.domain.TLaboratory;
import com.ruoyi.admin.domain.vo.ExperimentrequestVo;
import com.ruoyi.admin.domain.vo.LabtoryVo;
import com.ruoyi.admin.mapper.ExperimentrequestMapper;

import com.ruoyi.admin.domain.vo.LabscheduleVo;

import com.ruoyi.admin.mapper.SemesterMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.LabscheduleMapper;
import com.ruoyi.admin.domain.Labschedule;
import com.ruoyi.admin.service.ILabscheduleService;

import javax.annotation.Resource;

/**
 * 实验课安排Service业务层处理
 * 
 * @author lin
 * @date 2024-04-28
 */
@Service
@Slf4j
public class LabscheduleServiceImpl implements ILabscheduleService 
{
    @Autowired
    private LabscheduleMapper labscheduleMapper;
    @Resource
    private SemesterServiceImpl semesterService;
    @Resource
    private ExperimentrequestMapper experimentrequestMapper;


    /**
     * 查询可排课的时间段
     */
    @Override
    public List<LabtoryVo> schedule(Long requestId)
    {
        //根据request查课程申请
        ExperimentrequestVo experimentrequest = experimentrequestMapper.selectExperimentrequestByRequestId(requestId);
        List<LabtoryVo> valueLabtory = labscheduleMapper.getValueLabtory(experimentrequest);
        List<Long> usedLabtory = labscheduleMapper.getUsedLabtory(experimentrequest);

        //过滤出可排课的时间段
        // 将usedLabtory中的lab_id转换为一个Set，以便快速查找
        Set<Long> usedLabIds = usedLabtory.stream().collect(Collectors.toSet());

        // 使用流过滤掉valueLabtory中lab_id在usedLabIds中的元素
        List<LabtoryVo> filteredValueLabtory = valueLabtory.stream()
                .filter(lab -> !usedLabIds.contains(lab.getLabId()))
                .collect(Collectors.toList());

        // 现在filteredValueLabtory包含了没有在usedLabtory中出现的LabtoryVo对象
        log.info("可选的实验室数量数是{}",filteredValueLabtory.size());
        return filteredValueLabtory;
    }


    @Override
    public boolean doSchedule(Long labId,Long requestId) {
        ExperimentrequestVo experimentrequest = experimentrequestMapper.selectExperimentrequestByRequestId(requestId);
        Labschedule labschedule = new Labschedule();
        labschedule.setLabId(labId);
        labschedule.setWeekNumber(experimentrequest.getStartWeek()+"-"+experimentrequest.getEndWeek());
        Semester semester = semesterService.getCurrentSemester();
        labschedule.setSemesterId(semester.getSemesterId());
        labschedule.setTeacherId(experimentrequest.getTeacherId());
//        labschedule.setClassName(experimentrequest.getClassId());
        labschedule.setCourseName(experimentrequest.getCourseName());
        labschedule.setRequestId(requestId);
        labschedule.setClassPeriodId(experimentrequest.getClassPeriodId());
        labschedule.setDayOfWeek(1);
        if(labscheduleMapper.distinctRequest(requestId).size()<1&&labscheduleMapper.isLabFree(labId)==0){
            labscheduleMapper.insertLabschedule(labschedule);
            experimentrequestMapper.updateStatusToOne(requestId);
            return  true;
        }
        return false;
    }


    /**
     * 查询实验课安排
     * 
     * @param scheduleId 实验课安排主键
     * @return 实验课安排
     */
    @Override
    public LabscheduleVo selectLabscheduleByScheduleId(Long scheduleId)
    {
        return labscheduleMapper.selectLabscheduleByScheduleId(scheduleId);
    }

    /**
     * 查询实验课安排列表
     * 
     * @param labschedule 实验课安排
     * @return 实验课安排
     */
    @Override
    public List<LabscheduleVo> selectLabscheduleList(Labschedule labschedule)
    {
        return labscheduleMapper.selectLabscheduleList(labschedule);
    }

    /**
     * 新增实验课安排
     * 
     * @param labschedule 实验课安排
     * @return 结果
     */
    @Override
    public int insertLabschedule(Labschedule labschedule)
    {
        return labscheduleMapper.insertLabschedule(labschedule);
    }

    /**
     * 修改实验课安排
     * 
     * @param labschedule 实验课安排
     * @return 结果
     */
    @Override
    public int updateLabschedule(Labschedule labschedule)
    {
        return labscheduleMapper.updateLabschedule(labschedule);
    }

    /**
     * 批量删除实验课安排
     * 
     * @param scheduleIds 需要删除的实验课安排主键
     * @return 结果
     */
    @Override
    public int deleteLabscheduleByScheduleIds(Long[] scheduleIds)
    {
        return labscheduleMapper.deleteLabscheduleByScheduleIds(scheduleIds);
    }

    /**
     * 删除实验课安排信息
     * 
     * @param scheduleId 实验课安排主键
     * @return 结果
     */
    @Override
    public int deleteLabscheduleByScheduleId(Long scheduleId)
    {
        return labscheduleMapper.deleteLabscheduleByScheduleId(scheduleId);
    }


}
